home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Graphics / Gallery / Source / CompareWindow.cpp next >
Encoding:
C/C++ Source or Header  |  1997-11-20  |  8.1 KB  |  349 lines

  1. #include <stream.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <stdio.h>
  7.  
  8. #include <dos/stdio.h>
  9. #include <clib/dos_protos.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/utility_protos.h>
  12.  
  13. #include "GUIC_Application.hpp"
  14. #include "GUIC_OldButton.hpp"
  15. #include "GUIC_Checkbox.hpp"
  16. #include "GUIC_Cycle.hpp"
  17. #include "GUIC_Date.hpp"
  18. #include "GUIC_DirectoryExamine.hpp"
  19. #include "GUIC_Error.hpp"
  20. #include "GUIC_Event.hpp"
  21. #include "GUIC_Exceptions.hpp"
  22. #include "GUIC_File.hpp"
  23. #include "GUIC_FileString.hpp"
  24. #include "GUIC_FileExamine.hpp"
  25. #include "GUIC_Fillbar.hpp"
  26. #include "GUIC_Frame.hpp"
  27. #include "GUIC_GGFXPicture.hpp"
  28. #include "GUIC_Listview.hpp"
  29. #include "GUIC_PathString.hpp"
  30. #include "GUIC_Screen.hpp"
  31. #include "GUIC_SlidingInteger.hpp"
  32. #include "GUIC_String.hpp"
  33. #include "GUIC_StringType.hpp"
  34. #include "GUIC_System.hpp"
  35.  
  36. #include "CompareWindow.hpp"
  37. #include "PrefsWindow.hpp"
  38.  
  39. /*********************************************************************************************************/
  40.  
  41. class CompareWindowC_ListEntry : public GUIC_ObjectC
  42.     {
  43.     public:
  44.         CompareWindowC_ListEntry    (GUIC_FileExamineC *file);
  45.         ~CompareWindowC_ListEntry    (VOID);
  46.         STRPTR    getClass                    (VOID);
  47.         LONG        compare                    (GUIC_ObjectC &o);
  48.         VOID        print                        (VOID);
  49.         
  50.         STRPTR     fileName;
  51.         LONG        fileSize;
  52.         
  53.     protected:    
  54.         VOID        cleanUp                    (VOID);
  55.     };
  56.  
  57. CompareWindowC_ListEntry::CompareWindowC_ListEntry        (GUIC_FileExamineC *file)
  58. {
  59.     fileName    = 0;
  60.     fileSize    = file->getSize();
  61.  
  62.     GUIC_SystemC::reallocString(&fileName, file->getName());
  63. }
  64. CompareWindowC_ListEntry::~CompareWindowC_ListEntry    (VOID)
  65. {
  66.     if (fileName)    delete [] fileName;
  67. }
  68. STRPTR     CompareWindowC_ListEntry::getClass                    (VOID)
  69. {
  70.     return "CompareWindowC_ListEntry";
  71. }
  72. VOID        CompareWindowC_ListEntry::cleanUp                        (VOID)
  73. {
  74. }
  75. LONG        CompareWindowC_ListEntry::compare                    (GUIC_ObjectC &o)
  76. {
  77.     CompareWindowC_ListEntry *e = (CompareWindowC_ListEntry *)&o;
  78.     
  79.     if (fileSize > e->fileSize) return 1;
  80.     if (fileSize < e->fileSize) return -1;
  81.     
  82.     return Stricmp(fileName, e->fileName);
  83. }
  84. VOID        CompareWindowC_ListEntry::print                            (VOID)
  85. {
  86.     cout << "[" << getClass() << "," << this << ",Name=" << fileName << ",Size=" << fileSize << "]" << endl;
  87. }
  88.  
  89. /*********************************************************************************************************/
  90.  
  91. CompareWindowC::CompareWindowC                (GUIC_ApplicationC &a, GUIC_ScreenC &s, PrefsWindowC &p) : GUIC_WindowC (42,30)
  92. {
  93.     app                    = &a;
  94.     screen            = &s;
  95.     pWindow            = &p;
  96.     
  97.     ps_path            = new GUIC_PathStringC            ( 1, 1,40, 2, "RAM:");
  98.     lv_message    = new GUIC_ListviewC                ( 1, 4,40,20);
  99.     bt_start            = new GUIC_OldButtonC            ( 1,25, 8, 4, "_Start");
  100.     fb_status        = new GUIC_FillbarC                    (10,25,31, 4, 0);
  101.  
  102.     lv_message    -> setReadOnly(TRUE);
  103.     
  104.     add(ps_path);
  105.     add(lv_message);
  106.     add(bt_start);
  107.     add(fb_status);
  108.  
  109.     app -> addPrefs("PathToCompare", ps_path);
  110.     app -> addPrefs("CompareWindow", this);
  111.     
  112.     setTitle("Gallery - File Compare");
  113.     setGuideContext("CompareWindow");
  114.  
  115.     activate();
  116. }
  117. CompareWindowC::~CompareWindowC            (VOID)
  118. {
  119.     cleanUp();
  120. }
  121.  
  122. /*********************************************************************************************************/
  123.  
  124. STRPTR    CompareWindowC::getClass            (VOID)
  125. {
  126.     return "CompareWindowC";
  127. }
  128.  
  129. BOOL        CompareWindowC::action                (GUIC_EventC &e)
  130. {
  131.     static LONG oldPriority;
  132.  
  133.     switch (e.id)
  134.         {
  135.         case GUIC_GadgetEvent:
  136.             if (e.gadget == (GUIC_GadgetC *) bt_start)
  137.                 {
  138.                 GUIC_ListC fileList;
  139.  
  140.                 // Set the status to 0
  141.                 fb_status->set(0);
  142.                 // Change the Task's priority
  143.                 if (pWindow->cb_priority->get()) oldPriority = SetTaskPri(FindTask(NULL), pWindow->si_priority->get() );
  144.  
  145.                 try 
  146.                     {
  147.                     LONG equal; CHAR dummy[128];
  148.                     GUIC_DirectoryExamineC dir ( ps_path->get() );
  149.                 
  150.                     app->setBusy(TRUE);
  151.                     
  152.                     ps_path->setEnabled(FALSE);
  153.                     bt_start->set("Stop");
  154.                     lv_message->delAllItems();
  155.                     
  156.                     lv_message->addItem("Scanning directory.");
  157.                     setBusy(FALSE);
  158.                     scanDirectory(dir, fileList);
  159.                     setBusy(TRUE);
  160.                     lv_message->addItem("Directory scanned.");
  161.  
  162.                     lv_message->addItem("Comparing files:");
  163.                     lv_message->addItem(" ");
  164.                     setBusy(FALSE);
  165.                     equal = compareFiles(fileList);
  166.                     setBusy(TRUE);
  167.                     lv_message->addItem(" ");
  168.                     lv_message->addItem("Files compared.");
  169.                     sprintf(dummy, "%ld equal files found.", equal);
  170.                     lv_message->addItem(dummy);
  171.                     lv_message->addItem("Logfile saved to T:Gallery.log.");
  172.                     
  173.                     }
  174.                 catch (GUIC_Exception &ex)
  175.                     {
  176.                     GUIC_ErrorC err ("Error", ex.getMessage() );
  177.                     err.request(this);
  178.                     }
  179.  
  180.                 setBusy(TRUE);
  181.                 while (fileList.length()) delete (CompareWindowC_ListEntry *) fileList.remove(1);
  182.                 setBusy(FALSE);
  183.  
  184.                 ps_path->setEnabled(TRUE);
  185.                 bt_start->set("_Start");
  186.                 
  187.                 // Change the priority again
  188.                 if (pWindow->cb_priority->get()) SetTaskPri(FindTask(NULL), oldPriority );
  189.  
  190.                 app->setBusy(FALSE);
  191.                 }
  192.             return TRUE;
  193.             break;
  194.         case GUIC_OpenWindow:
  195.             return TRUE;
  196.             break;
  197.         case GUIC_CloseWindow:
  198.             screen->remove(this);
  199.             return TRUE;
  200.             break;
  201.         }
  202.     
  203.     return FALSE;
  204. }
  205.  
  206. /*********************************************************************************************************/
  207.  
  208. VOID        CompareWindowC::scanDirectory    (GUIC_DirectoryExamineC &dir, GUIC_ListC &list)
  209. {
  210.     LONG result=0;
  211.     CompareWindowC_ListEntry *entry;
  212.     
  213.     try
  214.         {
  215.         GUIC_FileExamineC *file = dir.examineNext();
  216.         while (file)
  217.             {
  218.             if (file->isDirectory())
  219.                 {
  220.                 GUIC_DirectoryExamineC nextDir (file->getName());
  221.                 scanDirectory(nextDir, list); 
  222.                 }
  223.             else 
  224.                 {
  225.                 entry = new CompareWindowC_ListEntry(file);
  226.                 list.addSorted(entry);
  227.                 result++;
  228.                 }
  229.             
  230.             GUIC_EventC *event = app->checkEvent();
  231.             if (event && event->id == GUIC_GadgetEvent) throw GUIC_Exception("User Abort");
  232.             
  233.             file = dir.examineNext();
  234.             }
  235.         }
  236.     catch (GUIC_Exception &) { throw; }
  237. }
  238. LONG        CompareWindowC::compareFiles    (GUIC_ListC &list)
  239. {
  240.     LONG found = 0, length = list.length(), oldLength=length;
  241.     CompareWindowC_ListEntry *entry1, *entry2;
  242.     GUIC_FileC file ("T:Gallery.log", GUIC_Write);
  243.     
  244.     while (length > 1)
  245.         {
  246.  
  247.         /* Check for Stop gadget */
  248.         GUIC_EventC *event = app->checkEvent();
  249.         if (event && event->id == GUIC_GadgetEvent) throw GUIC_Exception("User Abort");
  250.  
  251.         entry1 = (CompareWindowC_ListEntry *) list.objectAt(1);
  252.         entry2 = (CompareWindowC_ListEntry *) list.objectAt(2);
  253.         
  254.         if (entry1->fileSize == entry2->fileSize)
  255.             {
  256.             LONG i=2; 
  257.             BOOL writeIt = FALSE;
  258.             
  259.             while (entry1->fileSize == entry2->fileSize)
  260.                 {
  261.                 if (areEqual(entry1->fileName, entry2->fileName))
  262.                     {
  263.                     writeIt = TRUE;
  264.                     file.writeLn(entry2->fileName);
  265.                     lv_message->addItem(entry2->fileName);
  266.                     delete (CompareWindowC_ListEntry *) list.remove(i);         
  267.                     length--;
  268.                     if (length == 1 || i > length) break;
  269.                     entry2 = (CompareWindowC_ListEntry *) list.objectAt(i);
  270.                     }
  271.                 else 
  272.                     {
  273.                     i++;
  274.                     if (i > length) break;
  275.                     entry2 = (CompareWindowC_ListEntry *) list.objectAt(i);
  276.                     }
  277.                 fb_status->set( (100*(oldLength-length))/oldLength );
  278.                 }
  279.  
  280.             if (writeIt)
  281.                 {
  282.                 file.writeLn(entry1->fileName);
  283.                 file.writeLn("------------------------------");
  284.                 
  285.                 lv_message->addItem(entry1->fileName);
  286.                 lv_message->addItem("------------------------------");
  287.                 
  288.                 found++;
  289.                 }
  290.             }
  291.  
  292.         delete (CompareWindowC_ListEntry *) list.remove(1); 
  293.         length--;
  294.  
  295.         fb_status->set( (100*(oldLength-length))/oldLength );
  296.         }
  297.     
  298.     return found;
  299. }
  300. BOOL        CompareWindowC::areEqual            (STRPTR file1, STRPTR file2)
  301. {
  302.     int read1, read2;
  303.     GUIC_Exception *ex = 0;
  304.     FILE *fp1, *fp2;
  305.     
  306.     fp1 = fopen(file1, "rb");
  307.     if (fp1)
  308.         {
  309.         fp2 = fopen(file2, "rb");
  310.         if (fp2)
  311.             {
  312.             FOREVER
  313.                 {
  314.                 read1 = fgetc(fp1);
  315.                 read2 = fgetc(fp2);
  316.  
  317.                 if (read1 == EOF && read2 == EOF) break;
  318.                 
  319.                 if (read1 != read2 )
  320.                     {
  321.                     fclose(fp1);
  322.                     fclose(fp2);
  323.                     return FALSE;
  324.                     }
  325.                     
  326.                 }
  327.             fclose(fp2);
  328.             }
  329.         else ex = new GUIC_Exception ("Can't find file ", file2);
  330.         fclose(fp1);
  331.         }
  332.     else ex = new GUIC_Exception ("Can't find file ", file2);
  333.  
  334.     if (ex) throw ex;
  335.     
  336.     return TRUE;
  337. }
  338.  
  339. /*********************************************************************************************************/
  340.  
  341. VOID        CompareWindowC::cleanUp                (VOID)
  342. {
  343.     delete ps_path;
  344.     delete lv_message;
  345.     delete bt_start;
  346.     delete fb_status;
  347. }
  348.  
  349.